﻿using MCUPackage;
using MCUPackage.DesignEditor;
using MCUPackage.Utils;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing.Design;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SxPackages.STC32G.Models
{
	/// <summary>
	/// 系统配置
	/// </summary>
	[Serializable]
	class SysOptions
	{
		/// <summary>
		/// 所属选项
		/// </summary>
		protected STC32GQFP64 mParent = null;

		private string mFreq = "24.000";
		/// <summary>
		/// 系统主频
		/// </summary>
		[Category("系统配置"),
		DisplayName("系统主频"),
		DefaultValue("24.000"),
		Description("系统运行主频(MHz)"),
		Editor(typeof(FreqEditor), typeof(UITypeEditor))]
		public string Freq
		{
			get => this.mFreq;
			set
			{
				if (value != mFreq)
				{
					double freq = Convert.ToDouble(value);
					this.FreqValue = freq * 1000;
					mFreq = TemplateHelper.GetFreqStr(freq);
					mParent.UpdateMsg(mFreq, MCUMsgTypes.Freq);
				}
			}
		}
		/// <summary>
		/// 主频具体的值(KHz)
		/// </summary>
		[Browsable(false)]
		[JsonIgnore]
		public double FreqValue { get; private set; } = 24000;
		/// <summary>
		/// 看门狗配置
		/// </summary>
		[Category("系统配置"),
		DisplayName("看门狗"),
		Description("看门狗配置"),
		TypeConverter(typeof(PropertySorter))]
		public WDGOptions WDG { get; private set; }

		[Category("系统配置"),
		DisplayName("printf函数接口"),
		DefaultValue(PrintfPort.串口1),
		Description("printf函数输出默认使用哪个接口")]
		public PrintfPort Port { get; set; } = PrintfPort.串口1;

		/// <summary>
		/// 使能内置Flash
		/// </summary>
		[Category("系统配置"),
		DisplayName("使用EEPROM"),
		DefaultValue(false),
		Description("是否使用芯片内置EEPROM存储器")]
		public bool EnableFlash { get; set; }

		[Category("系统配置"),
		DisplayName("使能系统嘀嗒"),
		DefaultValue(true),
		Description("使用定时器0作为系统嘀嗒, 周期为1ms, 强制开启")]
		public bool EnableTick { get; } = true;

		public SysOptions(STC32GQFP64 aParent)
		{
			this.mParent = aParent;
			this.WDG = new WDGOptions(aParent);
		}

		public override string ToString()
		{
			return string.Empty;
		}

		/// <summary>
		/// 创建系统文档
		/// </summary>
		/// <param name="aPath"></param>
		/// <returns></returns>
		public bool Cenerate(string aPath)
		{
			// 创建数据类型文件
			string file = "SxTypes.h";
			string path = PrjPathTypes.Inc.ToFilePath(aPath, file);

			PrjTemplate.Append(PrjPathTypes.Inc, file);
			mParent.CreateByTemplate(PrjPathTypes.Inc, aPath, file, true, null, "常用数据类型定义");

			// 创建Config文件
			file = "Config.h";
			StringBuilder otherHeader = new StringBuilder();
			if (this.WDG.Enable)
			{
				otherHeader.AppendLine("#include \"STC32G_WDT.h\"");
			}
			if (mParent.mUSB.Enable && this.Port == PrintfPort.USB_HID)
			{
				otherHeader.AppendLine("#include \"stc32_stc8_usb.h\"");
			}
			var pairs = TemplateHelper.CreatePairs();
			pairs["Freq"] = Convert.ToString((UInt32)(this.FreqValue * 1000));
			pairs["OtherHeader"] = otherHeader.ToString();
			mParent.CreateByTemplate(PrjPathTypes.Inc, aPath, file, true, pairs, "系统配置文件, 其他源文件引用这一个就可以了");

			// 复制串口头文件
			file = "STC32G_UART.h";
			pairs.Clear();
			pairs["PrintfUart"] = (this.Port >= PrintfPort.串口1 && this.Port <= PrintfPort.串口2) ? $"UART{(int)this.Port}" : "0";
			foreach (var uart in mParent.mUarts.All)
			{
				pairs.EnableKey($"UseUart{uart.Id}", uart.Enable);
			}
			mParent.CreateByTemplate(PrjPathTypes.Drivers_STC32_Inc, aPath, file, false, pairs);

			// 复制USB头文件
			if (mParent.mUSB.Enable)
			{
				file = "stc32_stc8_usb.h";
				pairs.Clear();
				pairs.EnableKey("UseHID", this.Port == PrintfPort.USB_HID);
				mParent.CreateByTemplate(PrjPathTypes.Drivers_STC32_Inc, aPath, file, false, pairs);
			}

			return true;
		}
	}
}
